package com.design.springboot.booksharing.controller;

import com.design.springboot.booksharing.bean.ApiResult;
import com.design.springboot.booksharing.bean.BookClassification;
import com.design.springboot.booksharing.bean.vo.BookClassificationVo;
import com.design.springboot.booksharing.component.ApiResultComponent;
import com.design.springboot.booksharing.enums.ApiResultCodeEnum;
import com.design.springboot.booksharing.service.BookClassificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户信息表(BookClassification)控制器
 *
 * @author admin
 */
@Slf4j
@RestController
@RequestMapping("/bookClassification")
public class BookClassificationController {
    @Resource
    ApiResultComponent apiResultComponent;

    @Resource
    private BookClassificationService bookClassificationService;

    @PostMapping("/saveBookClassification")
    public ApiResult<BookClassification> saveBookClassification(BookClassification bookClassification) {
        Boolean saveBookClassification = bookClassificationService.saveBookClassification(bookClassification);
        if (saveBookClassification) {
            BookClassification bookClassificationById = bookClassificationService.getBookClassificationById(bookClassification.getId());
            return apiResultComponent.success(bookClassificationById);
        }
        return apiResultComponent.error(ApiResultCodeEnum.ADD_ERROR);
    }

    @PostMapping("/updateBookClassification")
    public ApiResult<Integer> updateBookClassification(BookClassification bookClassification) {
        Boolean updateBookClassification = bookClassificationService.updateBookClassification(bookClassification);
        if (updateBookClassification) {
            BookClassification bookClassificationById = bookClassificationService.getBookClassificationById(bookClassification.getId());
            return apiResultComponent.success(bookClassificationById);
        }
        return apiResultComponent.error(ApiResultCodeEnum.UPDATE_ERROR);
    }

    @PostMapping("/deleteBookClassification")
    public ApiResult<BookClassification> deleteBookClassification(Integer bookClassificationId) {
        BookClassification bookClassificationById = bookClassificationService.getBookClassificationById(bookClassificationId);
        if (bookClassificationById == null) {
            return apiResultComponent.error(ApiResultCodeEnum.USER_NULL);
        }
        bookClassificationService.deleteBookClassification(bookClassificationId);
        return apiResultComponent.success(bookClassificationById);
    }

    @GetMapping("/getBookClassificationById")
    public ApiResult<BookClassification> getBookClassificationById(Integer id) {
        try {
            BookClassification bookClassificationById = bookClassificationService.getBookClassificationById(id);
            return apiResultComponent.success(bookClassificationById);
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error(ex.getMessage(), ex);
            return apiResultComponent.error(ApiResultCodeEnum.ERROR);
        }
    }

    @GetMapping("/bookClassificationTreeVo")
    public ApiResult<List<BookClassification>> listBookClassification() {
        List<BookClassificationVo> list = bookClassificationService.bookClassificationTreeVo();
        return apiResultComponent.success(list);
    }
}
